import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 0,
    num: 34,
    inputValue: 'AAA',
    list: []
  },
  mutations: {
    addCount(state, step) {
      state.count += step
    },
    initList(state, list) {
      state.list = list
    },
    setInputValue(state, value) {
      state.inputValue = value
    }
  },
  actions: {
    addAsync(context, step) {
      setTimeout(() => {
        context.commit('addCount', step)
      }, 2000)
    },
    // 异步初始化请求
    getList(context) {
      axios.get('../public/list.json').then(({ data }) => {
        console.log(data)
        context.commit('initList', data)
      })
    }
  },
  getters: {
    showCount: state => state.count + 1,
    showCount2: function (state) {
      return state.num / 2
    }
  },
  modules: {
  }
})
